<template>
    <section class="wrapper">
        <el-dialog
            title="上传回执单"
            width="680px"
            :visible.sync="dialogVisible"
            @close="handleClose"
        >
            <el-form
                class="base-info contract"
                :model="ruleForm"
                :rules="rules"
                ref="ruleForm"
                label-position="right"
                label-width="96px"
            >
                <el-form-item label="企业名称">
                    <el-input
                        v-model="company_name"
                        size="medium"
                        clearable
                        style="width: 430px"
                        disabled
                    />
                </el-form-item>
                <el-form-item
                    label="退款金额"
                    prop="refund_money"
                >
                    <el-input
                        v-model="ruleForm.refund_money"
                        size="medium"
                        clearable
                        style="width: 430px"
                    >
                        <span
                            slot="suffix"
                            style="margin-right: 5px"
                        >
                            元
                        </span>
                    </el-input>
                </el-form-item>
                <el-form-item
                    label="上传回执单"
                    prop="fileList"
                >
                    <file-upload
                        v-model="ruleForm.fileList"
                        accept=".jpg,.jpeg,.png,.GIF,.JPG,.PNG"
                    />
                </el-form-item>
            </el-form>
            <div class="btn">
                <el-button
                    size="medium"
                    style="width: 120px;margin-right: 32px"
                    @click="dialogVisible = false"
                >
                    取消
                </el-button>
                <el-button
                    size="medium"
                    type="primary"
                    style="width: 120px"
                    :loading="loading"
                    @click="handleSubmit"
                >
                    确认
                </el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
import FileUpload from "./file-upload";
export default {
    name: "UploadRefund",
    components: {FileUpload},
    data() {
        return {
            dialogVisible: false,
            loading: false,
            company_name: '',
            ruleForm: {
                apply_no: '',
                // 回执单文件
                fileList: [],
                // 回执单链接
                refund_return_receipt: '',
                // 退款金额
                refund_money: ''
            },
            rules: {
                fileList: [
                    {required: true, message: '请上传协议', trigger: 'change'}
                ],
                refund_money: [
                    {required: true, message: '请填写退款金额', trigger: 'blur'}
                ]
            }
        };
    },
    methods: {
        /**
         * 显示弹窗
         * @param apply_no
         * @param company_name
         */
        show(apply_no, company_name) {
            this.ruleForm.apply_no = apply_no;
            this.company_name = company_name;
            this.dialogVisible = true;
        },

        /**
         * 提交
         */
        handleSubmit() {
            this.$refs.ruleForm.validate((valid) => {
                if(valid) {
                    this.loading = true;
                    let urls = this.ruleForm.fileList.map(item => {
                        return item.url;
                    });
                    let params = {
                        apply_no: this.ruleForm.apply_no,
                        refund_return_receipt: urls.join(),
                        refund_money: this.ruleForm.refund_money
                    };

                    this.$post('/student/shop_close_apply%5Cset_refund',params, resp => {
                        if(resp.code === 1) {
                            this.$notify({
                                title: '提示',
                                message: '上传成功',
                                type: 'success',
                                duration: 2000
                            });
                            this.$emit('update');
                            this.dialogVisible = false;
                        } else {
                            this.$notify({
                                title: '提示',
                                message: resp.msg,
                                type: 'warning',
                                duration: 2000
                            });
                        }
                        this.loading = false;
                    });
                }
            });
        },

        /**
         * 弹窗关闭
         */
        handleClose() {
            this.ruleForm.fileList = [];
            this.ruleForm.refund_return_receipt = '';
            this.ruleForm.refund_money = '';
        }
    }
};
</script>

<style scoped lang="less">
.wrapper {

    /deep/ .el-dialog__header {
        padding-top: 10px;
        box-shadow: 0 1px 0 0 rgba(233, 233, 233, 1);
        display: flex;
        align-items: center;
        background: #FAFAFA;
        border-radius: 4px 4px 0 0;

        .el-dialog__title {
            font-weight: 500;
            color: #333;
            font-size: 14px;
        }

        .el-icon-close:before {
            color: rgb(194, 194, 194);
            font-size: 17px;
            font-weight: bold;
        }

        .el-dialog__headerbtn {
            top: 14px;
        }
    }

    /deep/ .el-dialog__body {
        max-height: 500px;
        overflow: auto;
        &::-webkit-scrollbar {
            width: 6px;
        }
    }
    .btn {
        text-align: center;
    }
}
</style>
